package com.buoy.base.aluminumpricespider.scheduler;

import com.buoy.base.aluminumpricespider.config.SchedulerProperties;
import com.buoy.base.aluminumpricespider.service.SpiderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class PriceSpiderScheduler {

    private final SpiderService spiderService;
    private final SchedulerProperties schedulerProperties;

    @Autowired
    public PriceSpiderScheduler(SpiderService spiderService, SchedulerProperties schedulerProperties) {
        this.spiderService = spiderService;
        this.schedulerProperties = schedulerProperties;
    }

    /**
     * 每个工作日上午10:15爬取上海铝价
     */
    @Scheduled(cron = "${scheduler.shanghai.cron}")
    public void scheduleShanghaiPriceCrawl() throws IOException {
        if (!schedulerProperties.getShanghai().isEnabled()) {
            log.info("上海铝价爬取任务已禁用");
            return;
        }
        log.info("定时爬取上海铝价任务开始执行...");
        spiderService.crawlShanghaiPrice();
    }

    /**
     * 每个工作日上午10:40爬取长江铝价
     */
    @Scheduled(cron = "${scheduler.changjiang.cron}")
    public void scheduleChangjiangPriceCrawl() {
        if (!schedulerProperties.getChangjiang().isEnabled()) {
            log.info("长江铝价爬取任务已禁用");
            return;
        }
        log.info("定时爬取长江铝价任务开始执行...");
        spiderService.crawlChangjiangPrice();
    }
}